from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.kq_attendance_vo import Kq_attendancePageQueryModel, Kq_attendanceModel, \
    DeleteKq_attendanceModel
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.kq_attendance_service import Kq_attendanceService
from module_admin.service.login_service import LoginService

from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


kq_attendanceController = APIRouter(prefix='/kaoqin/kq_attendance', dependencies=[Depends(LoginService.get_current_user)])


@kq_attendanceController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_attendance:list'))]
)
async def get_kaoqin_kq_attendance_list(
    request: Request,
kq_attendance_page_query: Kq_attendancePageQueryModel = Depends(Kq_attendancePageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    kq_attendance_page_query_result = await Kq_attendanceService.get_kq_attendance_list_services(query_db, kq_attendance_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=kq_attendance_page_query_result)


@kq_attendanceController.post('', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_attendance:add'))])
@ValidateFields(validate_model='add_kq_attendance')
@Log(title='考勤数据表', business_type=BusinessType.INSERT)
async def add_kaoqin_kq_attendance(
    request: Request,
    add_kq_attendance: Kq_attendanceModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_kq_attendance.create_by = current_user.user.user_name
    add_kq_attendance.create_time = datetime.now()
    add_kq_attendance.update_by = current_user.user.user_name
    add_kq_attendance.update_time = datetime.now()
    add_kq_attendance_result = await Kq_attendanceService.add_kq_attendance_services(query_db, add_kq_attendance)
    logger.info(add_kq_attendance_result.message)

    return ResponseUtil.success(msg=add_kq_attendance_result.message)


@kq_attendanceController.put('', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_attendance:edit'))])
@ValidateFields(validate_model='edit_kq_attendance')
@Log(title='考勤数据表', business_type=BusinessType.UPDATE)
async def edit_kaoqin_kq_attendance(
    request: Request,
    edit_kq_attendance: Kq_attendanceModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_kq_attendance.update_by = current_user.user.user_name
    edit_kq_attendance.update_time = datetime.now()
    edit_kq_attendance_result = await Kq_attendanceService.edit_kq_attendance_services(query_db, edit_kq_attendance)
    logger.info(edit_kq_attendance_result.message)

    return ResponseUtil.success(msg=edit_kq_attendance_result.message)


@kq_attendanceController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_attendance:remove'))])
@Log(title='考勤数据表', business_type=BusinessType.DELETE)
async def delete_kaoqin_kq_attendance(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_kq_attendance = DeleteKq_attendanceModel(ids=ids)
    delete_kq_attendance_result = await Kq_attendanceService.delete_kq_attendance_services(query_db, delete_kq_attendance)
    logger.info(delete_kq_attendance_result.message)

    return ResponseUtil.success(msg=delete_kq_attendance_result.message)


@kq_attendanceController.get(
    '/{id}', response_model=Kq_attendanceModel, dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_attendance:query'))]
)
async def query_detail_kaoqin_kq_attendance(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    kq_attendance_detail_result = await Kq_attendanceService.kq_attendance_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=kq_attendance_detail_result)


@kq_attendanceController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_attendance:export'))])
@Log(title='考勤数据表', business_type=BusinessType.EXPORT)
async def export_kaoqin_kq_attendance_list(
    request: Request,
    kq_attendance_page_query: Kq_attendancePageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    kq_attendance_query_result = await Kq_attendanceService.get_kq_attendance_list_services(query_db, kq_attendance_page_query, is_page=False)
    kq_attendance_export_result = await Kq_attendanceService.export_kq_attendance_list_services(kq_attendance_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(kq_attendance_export_result))
